package br.com.fatecpg.mamaeconomiza;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import br.com.fatecpg.mamaeconomiza.library.ProductFunctions;

import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SearchActivity extends Activity {
	
	private ArrayList<HashMap<String, String>> productsList;
	private ArrayList<String> productPid;

	private ListView listAll;

	private static String KEY_SUCCESS = "success";
	private static String KEY_ERROR_MSG = "error_msg";
	private static String KEY_PID = "pid";
	private static String KEY_NAME = "name";
	private static String KEY_PRICE = "price";
	private static String KEY_MAKER = "maker";
	private static String KEY_DESCRIPTION = "description";
	private static String KEY_SHOP = "shop";
	private static String KEY_ADDRESS = "address";
	private static String KEY_CREATED_AT = "created_at";
	private static String KEY_UPDATE_AT = "update_at";


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		handleIntent(getIntent());
	}

	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
		handleIntent(intent);
		
	}
	
	private void handleIntent(Intent intent) {
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			String search = intent.getStringExtra(SearchManager.QUERY);
			
			
			listAll = (ListView) findViewById(R.id.listProducts);
			productsList = new ArrayList<HashMap<String, String>>();
			productPid = new ArrayList<String>();

			ProductFunctions productFunctions = new ProductFunctions();
			JSONObject json = productFunctions.searchProduct(search);

			try {

				if (json.getString(KEY_SUCCESS) != null) {

					String res = json.getString(KEY_SUCCESS);

					if (Integer.parseInt(res) == 1) {

						JSONArray json_prod = json.getJSONArray("products");

						for (int i = 0; i < json_prod.length(); i++) {
							JSONObject cont = json_prod.getJSONObject(i);

							String pid = cont.getString(KEY_PID);
							String name = cont.getString(KEY_NAME);
							String price = cont.getString(KEY_PRICE);

							HashMap<String, String> map = new HashMap<String, String>();

							map.put(KEY_NAME, name);
							map.put(KEY_PRICE, "R$ " + price);

							productsList.add(map);
							productPid.add(pid);

						}

						String[] from = new String[] { KEY_NAME, KEY_PRICE };
						int[] to = new int[] { R.id.textProduct, R.id.textPrice };

						SimpleAdapter listAdapter = new SimpleAdapter(SearchActivity.this, productsList, R.layout.list_item, from, to);

						listAll.setAdapter(listAdapter);

						listAll.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> adapter, View v,
									int position, long id) {

								String pid = productPid.get(position);
								Log.d("PIDs", pid);

								ProductFunctions productFunctions = new ProductFunctions();
								JSONObject json = productFunctions.getProduct(pid);

								try {

									if (json.getString(KEY_SUCCESS) != null) {

										String res = json.getString(KEY_SUCCESS);

										if (Integer.parseInt(res) == 1) {

											JSONObject json_prod = json.getJSONObject("product");

											Intent intent = new Intent(getApplicationContext(),	DetailsProductActivity.class);
											intent.putExtra("NAME",	json_prod.getString(KEY_NAME));
											intent.putExtra("PRICE", json_prod.getString(KEY_PRICE));
											intent.putExtra("MAKER", json_prod.getString(KEY_MAKER));
											intent.putExtra("DESCRIPTION", json_prod.getString(KEY_DESCRIPTION));
											intent.putExtra("ADDRESS", json_prod.getString(KEY_ADDRESS));
											intent.putExtra("SHOP", json_prod.getString(KEY_SHOP));
											
											startActivity(intent);

										}

									} else {
										Toast.makeText(getApplicationContext(),	KEY_ERROR_MSG, Toast.LENGTH_LONG).show();
									}

								} catch (JSONException e) {
									e.printStackTrace();
								}

							}
						});

					} else {
						Toast.makeText(getApplicationContext(),
								json.getString(KEY_ERROR_MSG), Toast.LENGTH_LONG)
								.show();

					}

				}

			} catch (JSONException e) {
				e.printStackTrace();
			} 
			
		}
		

	}
	

	
	

}
